import matplotlib.pyplot as plt
import numpy as np
from numpy import ma
from matplotlib import ticker, cm
#x,y及Z数据信息
N = 100
x = np.linspace(-3.0, 3.0, N)
y = np.linspace(-2.0, 2.0, N)
X, Y = np.meshgrid(x, y)#此处操作是为了将x,y的数据点数一致
Z1 = np.exp(-X**2 - Y**2)
Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
Z = (Z1 - Z2) * 2
#进行绘图
fig, ax = plt.subplots()
levels = np.arange(-2,2.0,0.005)#对颜色渐进细致程度进行设置，其中-2与2是色条显示的数据范围，0.005是颜色显示的细致程度
cs = ax.contourf(X, Y, Z, levels,cmap=plt.get_cmap('Spectral'))
#添加colorbar
cbar = fig.colorbar(cs)
plt.show()
